/*

This file is part of the replication package of "Asset-Price Redistribution" by
Fagereng, Gomez, Gouin-Bonenfant, Holm, Moll, and Natvik. 

This file produces a set of estimated effects on how income changes affect 
future asset purchases to correct the welfare effects for incomplete markets. 
It uses two setups: 
	1) Use income changes directly
	2) Use consumption changes predicted from income changes

*/

********************************************************************************
forval b_year = 1915/1991 { 
use lnr b_year *_wealth cons_excl_housing earned_income transfers *_total grunnbelop edlevel year using $data/apr_9319 if b_year == `b_year', clear


local reference_year = max(1995, `b_year' + 20)

* fix edlevel for the fillins
bys lnr: egen _temp = max(edlevel)
replace edlevel = _temp
drop _temp

* generate wealth percentile if early cohort
if `b_year' <= 1968 {
	bys b_year year (financial_wealth): gen _temp = (_n -1)/_N
	gen xtile_finwealth = floor(_temp*10)+1
	drop _temp
}


tsset lnr year
gen nf_income = (earned_income + transfers)*0.65
replace nf_income = 2*grunnbelop/1e3 if nf_income < 2*grunnbelop/1e3
tssmooth ma nf_income_avg = nf_income, window(1 1 1)
qui winsor2 nf_income_avg, replace cuts(0 99) by(year)
qui winsor2 nf_income, replace cuts(0 99) by(year)
gen lognfincome_winsorized = log(nf_income_avg)
gen lognfincome_winsorized_base = log(nf_income)

tsset lnr year
replace cons_excl_housing = 2*grunnbelop/1e3 if cons_excl_housing < 2*grunnbelop/1e3
qui winsor2 cons_excl_housing, replace cuts(0 99) by(year)
gen logc_winsorized_base = log(cons_excl_housing)



foreach var of varlist nt_housing_total deposits_total debt_total nt_equity_total {
preserve
qui winsor2 `var', replace cuts(1 99) by(year)

* control variables for late cohorts have no information of income and wealth -- only use education
if `b_year' <= 1968 {
	local controlvars i.edlevel i.xtile_finwealth lognfincome_winsorized
}
else {
	local controlvars i.edlevel
}


tempname postname1
postfile `postname1' horizon b_year beta se var using ${output}/incomplete_markets_income_`var'_`b_year', replace

tsset lnr year
local H = 24 - `reference_year' + 1995
forval h = 1/`H' {	
	di "`b_year' - `reference_year' - `var': increg: `h'"
	cap drop res_assetpurchase
	cap drop res_income

	* first residualized both the dep. and ind. variable (so that we get the right variance)
	qui reg f`h'.lognfincome_winsorized_base `controlvars' if b_year == `b_year' & year == `reference_year'
	qui predict res_income, res
	
	qui reg f`h'.`var' `controlvars' if b_year == `b_year' & year == `reference_year'
	qui predict res_assetpurchase, res
	
	* 2 do regression using residualized values 
	cap reg res_assetpurchase res_income if b_year == `b_year' & year == `reference_year', robust
	cap sum res_income if e(sample) == 1
	cap post `postname1' (`h') (`b_year') (_b[res_income]) (_se[res_income]) (r(Var))
	
	drop res_income res_assetpurchase 

}
postclose `postname1'




tempname postname2
postfile `postname2' horizon b_year beta se var using ${output}/incomplete_markets_cons_iv_`var'_`b_year', replace

tsset lnr year
local H = 24 - `reference_year' + 1995
forval h = 1/`H' {	
	di "`b_year' - `reference_year' - `var': ivreg: `h'"
	cap drop res_assetpurchase
	cap drop res_income
	cap drop res_consumption

	* first residualized both the dep. and ind. variable (so that we get the right variance)
	qui reg f`h'.lognfincome_winsorized_base `controlvars' if b_year == `b_year' & year == `reference_year'
	qui predict res_income, res
	
	qui reg f`h'.logc_winsorized_base `controlvars' if b_year == `b_year' & year == `reference_year'
	qui predict res_consumption, res
	
	qui reg f`h'.`var' `controlvars' if b_year == `b_year' & year == `reference_year'
	qui predict res_assetpurchase, res
	
	* 2 do regression using residualized values 
	
	cap ivregress 2sls res_assetpurchase (res_consumption = res_income) if b_year == `b_year' & year == `reference_year', robust
	cap sum res_consumption if e(sample) == 1
	cap post `postname2' (`h') (`b_year') (_b[res_consumption]) (_se[res_consumption]) (r(Var))
	
	drop res_income res_consumption res_assetpurchase 

}
postclose `postname2'

restore	
}
}

* rewrite all .dta files as .csv files
forval b_year == 1915/1991 { 
foreach var in nt_housing_total deposits_total debt_total nt_equity_total {
	use ${output}/incomplete_markets_income_`var'_`b_year'.dta, replace
	export delimited using ${output}/incomplete_markets_income_`var'_`b_year'.csv, replace
	erase ${output}/incomplete_markets_income_`var'_`b_year'.dta
	
	
	use ${output}/incomplete_markets_cons_iv_`var'_`b_year'.dta, replace
	export delimited using ${output}/incomplete_markets_cons_iv_`var'_`b_year'.csv, replace
	erase ${output}/incomplete_markets_cons_iv_`var'_`b_year'.dta
}
}
